package com.rjwh.dingdong.client.activity.guide;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;

import com.igexin.sdk.PushManager;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.BaseActivity;
import com.rjwh.dingdong.client.application.MyApplication;
import com.rjwh.dingdong.client.constant.LocalConstant;
import com.rjwh.dingdong.client.download.DownloadManager;
import com.rjwh.dingdong.client.fragment.menu.CommunityFragment;
import com.rjwh.dingdong.client.fragment.menu.MainPageFragment;
import com.rjwh.dingdong.client.fragment.menu.MoreFragment;
import com.rjwh.dingdong.client.fragment.menu.RadioPageFragment;
import com.rjwh.dingdong.client.service.AppPushIntentService;
import com.rjwh.dingdong.client.service.AppUpgradeServer;
import com.rjwh.dingdong.client.service.AppPushService;
import com.rjwh.dingdong.client.service.HttpWebCoreService;
import com.rjwh.dingdong.client.service.LocationService;
import com.rjwh.dingdong.client.util.AppUtil;
import com.rjwh.dingdong.client.util.LogUtil;
import com.rjwh.dingdong.client.util.UrlUtil;
import com.rjwh.dingdong.client.view.BottomBar;
import com.rjwh.dingdong.client.view.BottomBar.TabSelectImage;

public class MainActivity extends BaseActivity {

    private BottomBar bar;
    private int hostView;
    private long mExitTime = 0;
    private LocationService locationService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        parseIntent();
        initView();
        initService();
    }

    private void parseIntent() {
        Bundle bundle = getIntent().getBundleExtra(LocalConstant.APP_EXTRA_BUNDLE);
        if (bundle != null) {
            String actionurl = bundle.getString("actionurl");
            UrlUtil.handelUrl(this, actionurl, true);
            LogUtil.d("launchParam exists, redirect to DetailActivity");
        }
    }

    private void initView() {
        bar = (BottomBar) findViewById(R.id.bottombar);
        hostView = R.id.fragment_content;
        initBottomBar();
    }

    private void initService() {
        locationService = new LocationService(this);
        // 检测版本
        AppUpgradeServer.checkUpdata(this, false);
        //开启X5
        startX5WebService();
        //开启定位服务
        locationService.requestPeriodLocationUpdate();
        // 初始化推送sdk
        PushManager.getInstance().initialize(this.getApplicationContext(), AppPushService.class);
        PushManager.getInstance().registerPushIntentService(this.getApplicationContext(), AppPushIntentService.class);
        DownloadManager.getInstance();
    }

    private void startX5WebService() {
        try {
            PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), 0);
            // 当前版本的包名
            String packageNames = info.packageName;
            LogUtil.d("packageNames:" + packageNames);
            if (!AppUtil.isServiceRunning(this, packageNames + ".service.HttpWebCoreService")) {
                startService(new Intent(this, HttpWebCoreService.class));
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void initBottomBar() {
        bar.init(hostView, this);
        MoreFragment moreFragment = new MoreFragment();
        MainPageFragment mainPageFragment = new MainPageFragment();
        CommunityFragment communityFragment = new CommunityFragment();
        RadioPageFragment radioPageFragment = new RadioPageFragment();
        bar.addTab(new BottomBar.FragmentTab(mainPageFragment, 0, getString(R.string.tab_enlightenment_education), new TabSelectImage(R.drawable.tab_enlightenment_education_nor, R.drawable.tab_enlightenment_education_pre), true));
        bar.addTab(new BottomBar.FragmentTab(radioPageFragment, 1, getString(R.string.tab_radio), new TabSelectImage(R.drawable.tab_maths_game_nor, R.drawable.tab_maths_game_pre), false));
        bar.addTab(new BottomBar.FragmentTab(communityFragment, 2, getString(R.string.tab_community), new TabSelectImage(R.drawable.tab_community_nor, R.drawable.tab_community_pre), false));
        bar.addTab(new BottomBar.FragmentTab(moreFragment, 3, getString(R.string.tab_mine), new TabSelectImage(R.drawable.tab_mine_nor, R.drawable.tab_mine_pre), false));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        locationService.stopLocation();
        stopService(new Intent(this, HttpWebCoreService.class));
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                mExitTime = System.currentTimeMillis();
            } else {
                MyApplication.getInstance().onTerminate();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub
        super.onSaveInstanceState(outState);
    }

}
